# -*- encoding:utf-8 -*-

from abc import ABCMeta,abstractclassmethod
import time

import tkinter as tk
from tkinter import ttk

class MainFormCmds:
    vars = dict()
    def config(name):
        def foo(func):
            setattr(MainFormCmds,name,func)
        return foo
    def connectHw():
        raise NotImplementedError("MainFormCmds::connectHw()")
    def disconnectHw():
        raise NotImplementedError("MainFormCmds::disconnectHw()")

class MainForm:
    def __init__(self,title="Kvaser Tool",geo = "720x660"):
        self.cmds = {'connectHW':None,
            'unConnectHw':None,
            'BtnOpenFileOnClick':None,
            'startThread2':None,
            'sendWin':None,
            'treeviewClick':None}
        self.ctrls = dict()
        self.rt = tk.Tk()
        self.rt.title(title)
        self.rt.geometry(geo)
        self.build()
        self.printInfo("打开软件")
    
    def build(self):
        self.buildMenu()
        self.buildCHInfoBox()
        self.MessageBox()
        self.buildBDCMsgInfo()
        self.buildBDCMsgSelect()
        self.buildInfoBox()
    def show(self):
        self.rt.mainloop()
    
    def printInfo(self,info = "！未注明信息"):
        nowTime = time.strftime('%H:%M:%S', time.localtime())
        self.ctrls['showYouList'].insert(0,nowTime+'    '+info)

    def buildMenu(self):
        menubar = tk.Menu(self.rt)
        self.rt['menu'] = menubar
        menus = [tk.Menu(self.rt) for i in range(4)]
        submenus = tk.Menu(menus[3])
        # Menu: Link
        menubar.add_cascade(label="连接",menu=menus[0])
        menus[0].add_command(label="建立连接",command = self.cmds["connectHW"])
        menus[0].add_command(label="断开连接",command = self.cmds['unConnectHw'])
        # Menu: File
        menubar.add_cascade(label="文件",menu=menus[1])
        menus[1].add_command(label="载入DBC文件",command = self.cmds["BtnOpenFileOnClick"])
        menus[1].add_command(label="报文解析",command = self.cmds['startThread2'])
        # Menu: Message
        menubar.add_cascade(label="报文",menu=menus[2])
        menus[2].add_command(label="发送报文",command = self.cmds["sendWin"])
        # Menu: Config
        menubar.add_cascade(label='配置',menu=menus[3])
        menus[3].add_cascade(label='配置波特率',menu=submenus)
        submenus.add_command(label='125kb/s',command=lambda :setrate(canlib.canBITRATE_125K))
        submenus.add_command(label='250kb/s',command=lambda :setrate(canlib.canBITRATE_250K))
        submenus.add_command(label='500kb/s',command=lambda :setrate(canlib.canBITRATE_500K))
        submenus.add_command(label='1000kb/s',command=lambda :setrate(canlib.canBITRATE_1000K))

    def buildCHInfoBox(self):
        wid = 70
        hei = 1
        h2 = 2
        showInfoFra1=tk.LabelFrame(self.rt,height = 200,fg="LightSkyBlue", width=500,text='基本信息')
        showInfoFra1.grid(row=0,column=0,columnspan=2,sticky='w',padx=10,ipadx=3)
        (tk.Label(showInfoFra1, text= '通道')).grid(row=0, column=0)
        channelInfo1 = tk.Text(showInfoFra1, width=wid, height=hei)
        channelInfo1.grid(row=0, column=1, sticky='w')
        (tk.Label(showInfoFra1, text= 'EAN')).grid(row=1, column=0)
        channelInfo2 = tk.Text(showInfoFra1, width=wid, height=hei)
        channelInfo2.grid(row=1, column=1, sticky='w')
        (tk.Label(showInfoFra1, text= 'dbc地址',)).grid(row=2, column=0)
        dbcPath = tk.Text(showInfoFra1, width=wid, height=hei)
        dbcPath.grid(row=2, column=1, sticky='w')
    
    def MessageBox(self):
        showInfoFra2 = tk.LabelFrame(self.rt,height = 150,fg='LightSkyBlue', width=400,text='报文显示')
        showInfoFra2.grid(row=1,column=0,columnspan=2,padx=10,pady=10)

        scrollBar1 = tk.Scrollbar(showInfoFra2)
        scrollBar1.pack(side='right', fill='y')

        showMsgTree=ttk.Treeview(showInfoFra2,show="headings",yscrollcommand=scrollBar1.set,height=8)
        showMsgTree['columns']=('报文名称','报文ID','时间戳','长度','0','1','2','3','4','5','6','7',)
        showMsgTree.column('报文名称',width=80,anchor='center')
        showMsgTree.column('报文ID',width=110,anchor='center')
        showMsgTree.column('时间戳',width=110,anchor='center')
        showMsgTree.column('长度',width=90,anchor='center')
        showMsgTree.column('0',width=35,anchor='center')
        showMsgTree.column('1',width=35,anchor='center')
        showMsgTree.column('2',width=35,anchor='center')
        showMsgTree.column('3',width=35,anchor='center')
        showMsgTree.column('4',width=35,anchor='center')
        showMsgTree.column('5',width=35,anchor='center')
        showMsgTree.column('6',width=35,anchor='center')
        showMsgTree.column('7',width=35,anchor='center')

        showMsgTree.heading("报文名称",text="报文名称") #显示表头
        showMsgTree.heading("报文ID",text="报文ID")
        showMsgTree.heading("长度",text="长度")
        showMsgTree.heading('时间戳',text='时间戳')
        showMsgTree.heading("0",text="0")
        showMsgTree.heading("1",text="1")
        showMsgTree.heading("2",text="2")
        showMsgTree.heading("3",text="3")
        showMsgTree.heading("4",text="4")
        showMsgTree.heading("5",text="5")
        showMsgTree.heading("6",text="6")
        showMsgTree.heading('7',text='7')

        scrollBar1.config(command=showMsgTree.yview)
        showMsgTree.pack()

    def buildBDCMsgInfo(self):
        showInfoFra4 = tk.LabelFrame(self.rt,height = 200, fg="LightSkyBlue", width=400,text='信息显示')
        showInfoFra4.grid(row=2,column=1,padx=10,sticky='e')

        scrollBar2 = tk.Scrollbar(showInfoFra4)
        scrollBar2.pack(side='right', fill='y')

        showInfoTree=ttk.Treeview(showInfoFra4,show="headings",yscrollcommand=scrollBar2.set,height=7)

        showInfoTree['columns']=('信号名称','物理值','单位')
        showInfoTree.column('信号名称',width=200,anchor='center')
        showInfoTree.column('物理值',width=160,anchor='center')
        showInfoTree.column('单位',width=160,anchor='center')

        showInfoTree.heading("信号名称",text="信号名称") #显示表头
        showInfoTree.heading("物理值",text="物理值")
        showInfoTree.heading('单位',text='单位')

        scrollBar2.config(command=showInfoTree.yview)
        showInfoTree.pack()
    
    def buildBDCMsgSelect(self):
        showInfoFra3 = tk.LabelFrame(height = 200, fg="LightSkyBlue", width=200,text='信号列表')
        showInfoFra3.grid(row=2,column=0,padx=10)

        scrollBar3 = tk.Scrollbar(showInfoFra3)
        scrollBar3.pack(side='left', fill='y')

        choInfoTree=ttk.Treeview(showInfoFra3,show="headings",yscrollcommand=scrollBar3.set,height=7)
        choInfoTree.pack()

        choInfoTree['columns']=('1')
        choInfoTree.column('1',width=100,anchor='center')
        choInfoTree.heading('1',text='双击选择')

        scrollBar3.config(command=choInfoTree.yview)
        choInfoTree.bind('<Double-1>',self.cmds['treeviewClick'])
    
    def buildInfoBox(self):
        showInfoFra5=tk.LabelFrame(self.rt,width=650,height=10, fg="LightSkyBlue",text="操作信息")
        showInfoFra5.grid(row=3,column=0,padx=10,pady=10,columnspan=2)
        showInfoFra5.rowconfigure(0,weight=1)

        scrollBar5 = tk.Scrollbar(showInfoFra5)
        scrollBar5.pack(side='right', fill='y')

        showYouList=tk.Listbox(showInfoFra5,width=96,height=5,yscrollcommand=scrollBar5.set)
        showYouList.pack()

        scrollBar5.config(command=showYouList.yview)
        self.ctrls['showYouList'] = showYouList



class MainForm_Menu:
    def __init__(self,master):
        self.rt = master
        self.cmds = self.rt.cmds
        self.build()
    def build(self):
        menubar = tk.Menu(self.rt)
        self.rt.config(menu = menubar)
        # self.rt['menu'] = menubar
        menus = [tk.Menu(self.rt) for i in range(4)]
        submenus = tk.Menu(menus[3])
        # Menu: Link
        menubar.add_cascade(label="连接",menu=menus[0])
        menus[0].add_command(label="建立连接",
            command = None)
        menus[0].add_command(label="断开连接",
            command = None)
        # Menu: File
        menubar.add_cascade(label="文件",menu=menus[1])
        menus[1].add_command(label="载入DBC文件",
            command = None)
        menus[1].add_command(label="报文解析",
            command = None)
        # Menu: Message
        menubar.add_cascade(label="报文",menu=menus[2])
        menus[2].add_command(label="发送报文",
            command = None)
        # Menu: Config
        menubar.add_cascade(label='配置',menu=menus[3])
        menus[3].add_cascade(label='配置波特率',menu=submenus)
        submenus.add_command(label='125kb/s',command=None)
        submenus.add_command(label='250kb/s',command=None)
        submenus.add_command(label='500kb/s',command=None)
        submenus.add_command(label='1000kb/s',command=None)


if __name__ == "__main__":
    test = 2
    if test == 0:
        win = MainForm()
        win.show()
    elif test == 1:
        x = tk.Tk()
        m = MainFormMenu(x)
        x.mainloop()
    elif test ==2:
        @MainFormCmds.config("connectHw")
        def xdf():
            print("ok1")
        # setattr(MainFormCmds,"connectHw",lambda:print("ok"))
        MainFormCmds.connectHw()